package com.sgb.member.ui.fragment;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import com.google.android.material.tabs.TabLayout;
import com.sgb.member.MemberConfig;
import com.sgb.member.R;
import com.sgb.member.databinding.FragmentMemberLeaseBinding;
import com.swgk.core.base.AppLazyFragment;
import com.swgk.core.customview.XCustomTabLayout;
import com.swgk.core.view.selection.CommonPopEntity;
import java.util.ArrayList;
import java.util.List;

public class BusinessLeaseFragment extends AppLazyFragment {

    private FragmentMemberLeaseBinding mBinding;
    private List<Fragment> mFragments = new ArrayList<>();
    private List<CommonPopEntity> tab_top_data = new ArrayList<>();
    private BusinessLeaseFragment1 businessLeaseFragment1;
    private BusinessLeaseFragment2 businessLeaseFragment2;
    private String flag="1";

    public static BusinessLeaseFragment newInstance() {
        BusinessLeaseFragment fragment = new BusinessLeaseFragment();
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_member_lease, container, false);
        mBinding.setLifecycleOwner(this);
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        tab_top_data.add(new CommonPopEntity("1", "供方出租"));
        if (MemberConfig.getInstance().getComType() == MemberConfig.WEBSITE_FOR_PERSONAL) {
            tab_top_data.add(new CommonPopEntity("2", "个人出租"));
        } else {
            tab_top_data.add(new CommonPopEntity("2", "公司出租"));
        }
        setTapTabLayoutData();
        businessLeaseFragment1 = BusinessLeaseFragment1.newInstance();
        businessLeaseFragment2= BusinessLeaseFragment2.newInstance();
        mFragments.add(businessLeaseFragment1);
        mFragments.add(businessLeaseFragment2);
        mBinding.viewPager.setAdapter(new FragmentStatePagerAdapter(getChildFragmentManager()) {
            @NonNull
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return mFragments.size();
            }

            @Override
            public int getItemPosition(Object object) {
                return POSITION_NONE;
            }

            @Nullable
            @Override
            public CharSequence getPageTitle(int position) {
                return tab_top_data.get(position).getPickerViewText();
            }
        });

    }

    @Override
    protected void onLazyLoad() {
    }

    public void setTapTabLayoutData() {
        for (int i = 0; i < tab_top_data.size(); i++) {
            mBinding.tabTop.addTab(mBinding.tabTop.newTab().setText(tab_top_data.get(i).getTypeName()));
        }
        mBinding.tabTop.setTabMode(TabLayout.MODE_FIXED);
        mBinding.tabTop.setNormalColor(R.color.color_F5F5F5);
        mBinding.tabTop.setSelectColor(R.color.color_FFFFFF);
        mBinding.tabTop.setNormalSize(15);
        mBinding.tabTop.setSelectSize(18);
        mBinding.tabTop.addOnTabSelectedListener(tab_top_data, new XCustomTabLayout.SimpleSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                flag =tab_top_data.get(tab.getPosition()).getNo();
                mBinding.viewPager.setCurrentItem(tab.getPosition());
            }
        });
    }

    public void loadData(boolean isLoadMore) {
         if(flag.equals("1")){
             businessLeaseFragment1.loadData(isLoadMore);
         }else {
             businessLeaseFragment2.loadData(isLoadMore);
         }
    }

}
